package com.study02.zuichangzifuchuan;



/**
 * @author : wwl
 * @date : 2022/3/15 21:52:15
 */
public class TheLongestSubHuiWen {
    public static void main(String[] args) {
        String s = "aacabdkacaa";
        String s1 = subStr(s);
        System.out.println(s1);
    }
    public static String subStr(String s){
        int strLen = s.length();
        if (strLen < 2) {
            return s;
        }
        char[] str = s.toCharArray(); //把字符串化成单个字符
        Boolean[][] bs = new Boolean[strLen][strLen];//在内存中开辟一个二位字布尔类型的数组方形矩阵
        //令对角线一组为true 应为单个字符判断无意义
        for (int i = 0,j =0; i < strLen; i++,j++) {
            bs[i][j] = true;
        }
        int maxLen = 1,begin = 0;
        for (int L = 2; L < strLen; L++) {
            for (int i = 0; i < strLen; i++) {
                int j = L+i-1;
                if(j>=strLen)
                    break;
                if(str[i] != str[j]){
                    bs[i][j] = false;
                }else {
//                    if(j-i<3){
//                        bs[i][j] = true;
//                    }else {
//                        bs[i][j] = bs[i + 1][j - 1];
//                    }
                    bs[i][j] = true;

                }
                if (bs[i][j] && j - i +1 > maxLen){
                    maxLen = j-i+1;
                    begin = i;
                }
            }
        }
        return s.substring(begin,maxLen+begin);
    }
}
